<template>
  <el-dialog :title="dTitle" :visible.sync="visible" width="70%" :before-close="closeDialog">
    <el-form :rules="rules" :model="formInline" ref="formInline" class="form-style form" label-width="100px">
      <el-form-item label="名称:" prop="SupplierName">
        <el-input v-model="formInline.SupplierName" placeholder=" 请输入供应商名称"></el-input>
      </el-form-item>
      <el-form-item label="地址:" prop="Address">
        <el-cascader
          v-model="formInline.Address"
          :options="options"
          @change="handleChange"
          style="width:100%"
        ></el-cascader>
      </el-form-item>
      <el-form-item label="详细地址:" prop="SupplierAddress">
        <el-input placeholder="请输入详细地址/门牌号等" v-model="formInline.SupplierAddress"></el-input>
      </el-form-item>
      <el-form-item label="登录名:" prop="UserName">
        <el-input placeholder="登录名必须为字母+数字" v-model="formInline.UserName"></el-input>
      </el-form-item>
      <el-form-item label="提现周期:" prop="value5">
        <el-select v-model="formInline.value5" placeholder="请选择" class="spselect">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button style="margin-left: 100px" @click="visible = false">取消</el-button>
        <el-button @click="upload('ruleForm')">保存</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { AddSupplier, LoadSuppliers } from "@/request/api";
export default {
  props: {
    dTitle: {
      type: String,
      default: "",
    },
    visible: {
      type: Boolean,
      default: false,
    },
    formData: {
      type: Object,
      default: {},
    },
  },
  watch: {
    formData(newVal, oldVal) {
      if (newVal) {
        this.formInline = { ...newVal };
      }
    },
  },
  methods: {
    closeDialog(reload) {
      this.$emit("closeDialog", reload);
    },
    upload() {
      this.$refs["formInline"].validate(async (vali) => {
        if (vali) {
          AddSupplier(this.formInline)
            .then((res) => {
          // if(this.formInline.UserName === res.)
              if (res.StatusCode === 200) {
                this.$confirm("该条信息编辑成功！", "编辑成功", {});
                this.$parent.getTableDatas();
              }
            })
            .catch((err) => {
              console.log(err);
            });
        }
      });
    },
    handleChange() {
      console.log("sss");
    },
  },
  data() {
    return {
      formInline: {},
      options: [],
      rules: {
        SupplierName: [
          { required: true, message: "名称不能为空", trigger: "blur" },
          {
            min: 5,
            message: "长度不得少于 5 个字符",
            trigger: "blur",
          },
        ],
        UserName: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        SupplierAddress: [
          { required: true, message: "请输入详细地址/门牌号", trigger: "blur" },
        ],
      },
    };
  },
  mounted() {
    this.$nextTick(function () {
      this.formInline = this.formData;
    });
  },
};
</script>

<style lang="less" scoped>
.form {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;

.el-form-item {
  width: 80%;
}
}
</style>